package com.zhkj.txg.module.order.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.order.adapter.AddressListAdapter
import com.zhkj.txg.module.order.vm.AddressViewModel
import kotlinx.android.synthetic.main.activity_address_manager.*

class AddressManagerActivity : ViewModelActivity<AddressViewModel>() {

    companion object {
        private val TYPE_SELECTE = 0
        private val TYPE_NORMAL = 1
        private val TYPE = "type"
        fun selectAddress(activity: Activity, requestCode: Int) {
            val intent = Intent(activity, AddressManagerActivity::class.java)
                .putExtra(TYPE, TYPE_SELECTE)
            activity.startActivityForResult(intent, requestCode)
        }

        fun selectAddress(fragment: Fragment, requestCode: Int) {
            val intent = Intent(fragment.requireContext(), AddressManagerActivity::class.java)
                .putExtra(TYPE, TYPE_SELECTE)
            fragment.startActivityForResult(intent, requestCode)
        }

        fun open(activity: Activity) {
            val intent = Intent(activity, AddressManagerActivity::class.java)
                .putExtra(TYPE, TYPE_NORMAL)
            activity.startActivity(intent)
        }
    }

    override fun setViewModel(): AddressViewModel = createViewModel()
    override val layoutResId: Int = R.layout.activity_address_manager
    override fun statusTitleBar(): View? = titleAddress

    private val REQUEST_REFRESH: Int = 100
    private val adapter = AddressListAdapter()

    override fun initUI() {
        rvAddress.layoutManager = LinearLayoutManager(this)
        rvAddress.adapter = adapter
        adapter.setEmptyView(R.layout.layout_empty_data)
        btnAddAddress.setOnClickListener {
            AddressEditActivity.addAddress(this, REQUEST_REFRESH)
        }
        adapter.setOnItemClickListener { _, _, position ->
            val entity = adapter.data[position]
            if (intent.getIntExtra(TYPE, TYPE_NORMAL) == TYPE_NORMAL) {
                AddressEditActivity.editAddress(this, entity.id, REQUEST_REFRESH)
            } else {
                val intent = intent.putExtra("data", entity)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }
        refreshAddress.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.addressList(AccountManager.getToken()).takeIf {
            !it.hasObservers()
        }?.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                refreshAddress.finishRefresh()
                adapter.setNewData(it.response?.data)
            } else if (it.status == HttpResponseStatus.Status.FAILURE)
                refreshAddress.finishRefresh(false)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_REFRESH && resultCode == Activity.RESULT_OK) {
            initData()
        }
    }
}